﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using TestLogMonitor.View;
using TestLogMonitor.Common;
using System.IO;

namespace TestLogMonitor
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private Mutex _mutex;

        protected override void OnStartup(StartupEventArgs e)
        {
          
            // 创建命名互斥体
            const string mutexName = "MyAppMutex";
            bool createdNew;
            _mutex = new Mutex(true, mutexName, out createdNew);

            // 检查是否创建了新互斥体
            if (!createdNew)
            {
                // 如果互斥体已经存在，则退出应用程序
                MessageBox.Show("应用程序已经在运行中！");
                Current.Shutdown();
                return;
            }

            // 正常启动应用程序
            base.OnStartup(e);

            //加载全局配置文件
            //GlobalInfo.AppConfigs = BusinessProcess.DeserializeFromXml<ConfigInfo>(Path.Combine(Environment.CurrentDirectory, "AppConfig.cfg"));
        }

        protected override void OnExit(ExitEventArgs e)
        {
            // 释放互斥体
            _mutex?.ReleaseMutex();
            _mutex?.Dispose();

            base.OnExit(e);
        }
    }
}
